package zhongbo.javaweb.instagram.service.model;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.javaweb.instagram.dao.TaskFarDao;
import zhongbo.javaweb.instagram.model.TaskFar;
import zhongbo.javaweb.instagram.util.DateFormatUtil;

import java.util.Date;

/**
 * 说明：
 *
 * @author zhongboWork
 * @version 1.0
 * @since 2021-12-21 11:31
 */
@Service
@Slf4j
public class TaskFarService extends BaseService<TaskFar> {
    @Autowired
    private TaskFarDao TaskFarDao;
    private String taskNamePattern = "task_yyyyMMddHHmmss";

    @Override
    public TaskFar findOne(String taskId) {
        TaskFar task;
        try {
            task = TaskFarDao.getById(taskId);
        } catch (Exception e) {
            String msg = "获取 TaskFar 失败!";
            throw new RuntimeException(msg, e);
        }
        if (task == null) {
            return null;
        }

        String taskName = task.getTaskName();
        if (taskName == null) {
            taskName = DateFormatUtil.format(new Date(), taskNamePattern);
            task.setTaskName(taskName);
        }

        int timeoutExecTime = task.getTimeoutExecTime();
        if (timeoutExecTime <= 0) {
            timeoutExecTime = 10000;
            task.setTimeoutExecTime(timeoutExecTime);
        }

        int first = task.getFirst();
        if (first <= 0) {
            first = 40;
            task.setFirst(first);
        }
        int threadSize = task.getThreadSize();
        if (threadSize <= 0) {
            threadSize = 1;
            task.setThreadSize(threadSize);
        }

        return task;
    }
}
